<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 3] 3.10 C++ Features Not Found in Java</TITLE>
<META NAME="author" CONTENT="David Flanagan">
<META NAME="date" CONTENT="Thu Jul 31 15:50:04 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="Java">
<META NAME="title" CONTENT="Java in a Nutshell">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java in a Nutshell" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch03_09.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 3<br>Classes and Objects in Java</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch03_11.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JNUT2-CH-3-SECT-10">3.10 C++ Features Not Found in Java</A></h2>

<P CLASS=para>
<A NAME="CH3.FOS"></A>Throughout this chapter, we've noted similarities and
differences between Java and C++ in footnotes.  Java shares
enough concepts and features with C++ to make it an easy
language for C++ programmers to pick up.  There are several
features of C++ that have no parallel in Java, however.  In
general, Java does not adopt those features of C++ that make
the language significantly more complicated.  These
omissions from Java (or simplifications of C++) are 
described below.

<P CLASS=para>
C++ supports "multiple inheritance" of method
implementations from more than one superclass at a time.
While this seems like a very useful feature, adding it to
the language actually turns out to introduce many 
complexities.  The Java language designers chose to avoid the
added complexity by using interfaces instead.  Thus, a class in
Java can only inherit method implementations from a single
superclass, but it can inherit method declarations from any
number of interfaces.  In practice, this is not any
particular hardship.

<P CLASS=para>
C++ supports (though not yet in a very standardized way)
templates that allow you, for example, to implement a
<tt CLASS=literal>Stack</tt> class and then instantiate it as
<tt CLASS=literal>Stack&lt;int&gt;</tt> or <tt CLASS=literal>Stack&lt;double&gt;</tt> to produce two
separate types: a stack of integers and a stack of
floating-point values.  Java has no such facility.  However,
the fact that every class in Java is a subclass of
<tt CLASS=literal>Object</tt> means that every object can be cast to an
instance of <tt CLASS=literal>Object</tt>.  Thus, in Java, it is often
sufficient to define a data structure (such as a
<tt CLASS=literal>Stack</tt> class) that operates on <tt CLASS=literal>Object</tt>
values--the objects can be cast back to their actual type
whenever necessary.

<P CLASS=para>
C++ allows you to define operators that perform arbitrary
operations on instances of your classes.  In effect, it
allows you to extend the syntax of the language.  This is a
nifty feature, called operator overloading, that makes for 
very elegant examples.  In practice, however, it tends to 
make code hard to understand.  After much debate, the Java 
language designers decided to omit 
such "operator overloading" from the language.  
Note, though, that the use of the <tt CLASS=literal>+</tt> operator for 
string concatenation in Java is at least reminiscent of 
operator overloading.

<P CLASS=para>
C++ allows you to define "conversion functions" for a class
that automatically invoke an appropriate constructor method
when a value is assigned to a variable of that class.  This
is simply a syntactic shortcut (similar to overriding the
assignment operator) and is not included in Java.

<P CLASS=para>
In C++, objects are by default manipulated by value; you must use
<tt CLASS=literal>&amp;</tt> to specify a variable or function argument that
is automatically manipulated by reference.  In Java,
all objects are manipulated by reference, so there is no
need for this <tt CLASS=literal>&amp;</tt> syntax.

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch03_09.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch03_11.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>Abstract Classes and Interfaces</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>Summary</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
